﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Manage
{
    public partial class ServiceCategory : PageBaseBackend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindRepeaterServiceCategory();
                BindActiveCategory();
                if (Request["categoryID"] != null)
                {
                    InitData();
                }
            }
        }

        private void BindActiveCategory()
        {
            ddlCategory.DataSource = BLL.ServiceCategoryBLL.Instance.GetActiveList();
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "Sequence";
            ddlCategory.DataBind();
            ddlCategory.Items.Insert(0, new ListItem("=位置不变=","0"));
        }

        //初始化要修改的数据
        private void InitData()
        {
            string categoryid = Request["categoryID"];
            Model.ServiceCategory category = BLL.ServiceCategoryBLL.Instance.GetDetail(categoryid);
            if (category != null)
            {
                TextBoxCategoryName.Text = category.CategoryName;
                txtColor.Text = category.Color;
                ButtonAddCategory.Text = "修改";
            }
        }

        private void BindRepeaterServiceCategory()
        {
            IList<AUV5.Model.ServiceCategory> list = AUV5.BLL.ServiceCategoryBLL.Instance.GetList();
            this.RepeaterServiceCategory.DataSource = list;
            this.RepeaterServiceCategory.DataBind();
        }


        //提交信息，添加或修改
        protected void ButtonAddCategory_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.TextBoxCategoryName.Text.Trim()))
            {
                base.Alert("请输入类型名称！");
            }
            else
            {
                Model.ServiceCategory category = new Model.ServiceCategory();
                if (Request["categoryID"] != null)
                {
                    category = BLL.ServiceCategoryBLL.Instance.GetDetail(Request["categoryID"]);
                }
                category.CategoryName = this.TextBoxCategoryName.Text.Trim();
                category.Color = txtColor.Text.Trim().ToLower();
                if (Request["categoryID"] != null)
                {
                    category.Sequence= GetSequence(category.Sequence);
                    category.UpdateDate = DateTime.Now;
                    BLL.ServiceCategoryBLL.Instance.Update(category);
                }
                else
                {
                    category.Sequence= GetSequence(-1);
                    BLL.ServiceCategoryBLL.Instance.Add(category);
                }

                Response.Redirect("ServiceCategory.aspx");
            }
        }

        //计算服务类别的所处位置
        private double GetSequence(double oldsequence)
        { 
            double sequence = 0;
            double selectedvalue = double.Parse(ddlCategory.SelectedValue);
            if (ddlCategory.SelectedIndex == 0)
            {
                sequence = oldsequence; 
            }
            else if (ddlCategory.SelectedIndex == 1)
            {
                sequence = selectedvalue / 2;
            }
            else if (ddlCategory.SelectedIndex == ddlCategory.Items.Count - 1)
            {
                sequence = selectedvalue + 1;
            }
            else
            {
                double prevalue = double.Parse(ddlCategory.Items[ddlCategory.SelectedIndex - 1].Value);
                sequence = (selectedvalue + prevalue) / 2;
            }
            return sequence;
        }

        //启用--禁用
        protected void RepeaterServiceCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string categoryid = e.CommandArgument.ToString();
            Model.ServiceCategory category = BLL.ServiceCategoryBLL.Instance.GetDetail(categoryid);
            if (category != null)
            {
                if (e.CommandName == "Disable")
                {
                    category.IsDel = (int)Common.Enum.auvDel.Del;
                }
                else if(e.CommandName=="Enable")
                {
                    category.IsDel = (int)Common.Enum.auvDel.NotDel;
                }
                BLL.ServiceCategoryBLL.Instance.Update(category);
            }
            Response.Redirect("ServiceCategory.aspx");
        }
    }

}